package http

import (
	"gitee.com/captials-team/ubdframe/src/domain/configstc"
	"gitee.com/captials-team/ubdframe/src/domain/dto"
	"gitee.com/captials-team/ubdframe/src/domain/dto/paginate"
	"gitee.com/captials-team/ubdframe/src/domain/dto/reqdata"
	"gitee.com/captials-team/ubdframe/src/domain/dto/respdata"
	"gitee.com/captials-team/ubdframe/src/domain/interfaces"
	"gitee.com/captials-team/ubdframe/src/domain/models"
	"gitee.com/captials-team/ubdframe/src/pkg/gin_http"
	v1log "gitee.com/captials-team/ubdframe/src/pkg/logs"
	"github.com/gin-gonic/gin"
	"go.uber.org/dig"
)

type OrganizeManageController struct {
	l    v1log.ILog
	conf *configstc.AdminAppConfig

	orgSrv interfaces.ItfOrganizeService

	gin_http.ResponseController
}

func NewOrganizeManageController(di *dig.Scope, l v1log.ILog, conf *configstc.AdminAppConfig, orgSrv interfaces.ItfOrganizeService) *OrganizeManageController {
	ctr := &OrganizeManageController{
		l:      l,
		conf:   conf,
		orgSrv: orgSrv,
	}

	return ctr
}

// SearchOrganize godoc
// @Summary      组织列表
// @Description  搜索组织列表
// @Tags         admin.organize
// @Security     AdminKeyAuth
// @Produce      json
// @Param        param    body     reqdata.SearchOrganizeReq  true  "搜索参数集"
// @success 0  {object} respdata.ResponseData{data=respdata.SearchListResponse{list=[]models.Organize}} "获取成功"
// @Router       /mag/organize/search [post]
func (ctr *OrganizeManageController) SearchOrganize(ctx *gin.Context) {
	var req reqdata.SearchOrganizeReq
	ctx.ShouldBind(&req)

	params := dto.SearchOrganizeParams{
		Keywords: req.Keywords,
		Pid:      req.Pid,
		PType:    req.PType,
		Status:   req.Status,
	}
	list, pa, err := ctr.orgSrv.Search(params, paginate.NewPager(req.Page, req.Size))
	if err != nil {
		ctr.l.Error("search err %s", err)
		ctr.Response(ctx, respdata.CSysBusy)
		return
	}
	ctr.Response(ctx, respdata.CSuccess.MData(respdata.SearchListResponse{
		List:     list,
		Paginate: *pa,
	}))
}

// TreeOrganize godoc
// @Summary      组织树
// @Description  搜索组织树
// @Tags         admin.organize
// @Security     AdminKeyAuth
// @Produce      json
// @Param        param    body     reqdata.TreeOrganizeReq  true  "搜索参数集"
// @success 0  {object} respdata.ResponseData{data=respdata.SearchListResponse{list=[]dto.SwaggerTreeNode}} "获取成功"
// @Router       /mag/organize/tree [post]
func (ctr *OrganizeManageController) TreeOrganize(ctx *gin.Context) {
	var req reqdata.TreeOrganizeReq
	ctx.ShouldBind(&req)

	params := dto.TreeOrganizeParams{
		Pid:   req.Pid,
		PType: req.PType,
	}
	tree, err := ctr.orgSrv.Tree(params)
	if err != nil {
		ctr.l.Error("search err %s", err)
		ctr.Response(ctx, respdata.CSysBusy)
		return
	}
	ctr.Response(ctx, respdata.CSuccess.MData(tree))
}

// QueryOrganize godoc
// @Summary      查看详请
// @Description  获取组织详细信息
// @Security     AdminKeyAuth
// @Tags         admin.organize
// @Accept       json
// @Produce      json
// @Param        param    body     reqdata.DetailReq  true  "查询指定id详情"
// @success 0  {object} respdata.ResponseData{data=respdata.OrganizeDetail} "获取成功"
// @failure 2  {object} respdata.ResponseData{} "系统错误"
// @failure 4  {object} respdata.ResponseData{} "参数错误"
// @failure 8  {object} respdata.ResponseData{} "操作失败"
// @Router       /mag/organize/detail [post]
func (ctr *OrganizeManageController) QueryOrganize(ctx *gin.Context) {
	var req reqdata.DetailReq

	ctx.ShouldBind(&req)
	if req.Id <= 0 {
		ctr.Response(ctx, respdata.CParamsInvalid)
		return
	}

	info, err := ctr.orgSrv.Query(req.Id)
	if err != nil {
		ctr.l.Error("query err %s", err)
		ctr.Response(ctx, respdata.CSysBusy)
		return
	}

	ctr.Response(ctx, respdata.CSuccess.MData(info))
}

// SaveOrganize godoc
// @Summary      组织新增/更新
// @Description  新增组织，编辑提交组织
// @Security     AdminKeyAuth
// @Tags         admin.organize
// @Accept       json
// @Produce      json
// @Param        param    body     reqdata.AddEditOrganizeReq  true  "新增/编辑信息"
// @success 0  {object} respdata.ResponseData{data=models.Organize} "处理成功"
// @failure 2  {object} respdata.ResponseData{} "系统错误"
// @failure 4  {object} respdata.ResponseData{} "参数错误"
// @failure 8  {object} respdata.ResponseData{} "操作失败"
// @Router       /mag/organize/save [post]
func (ctr *OrganizeManageController) SaveOrganize(ctx *gin.Context) {
	var req reqdata.AddEditOrganizeReq
	ctx.ShouldBind(&req)

	data := models.Organize{
		Name:        req.Name,
		Description: req.Describe,
		Type:        "",
		Pid:         req.Pid,
		Status:      req.Status,
	}

	var err error
	if req.Id > 0 {
		_, err = ctr.orgSrv.Update(req.Id, data)
	} else {
		_, err = ctr.orgSrv.Add(data)
	}

	if err != nil {
		ctr.l.Error("save err %s", err)
		ctr.Response(ctx, respdata.CSysBusy)
		return
	}

	ctr.Response(ctx, respdata.CSuccess)
}

// DisableOrganize godoc
// @Summary      启用/禁用
// @Description  操作启用/禁用指定组织
// @Security     AdminKeyAuth
// @Tags         admin.organize
// @Accept       json
// @Produce      json
// @Param        param    body     reqdata.SetStatusReq  true  "状态设置请求"
// @success 200  {object} respdata.ResponseData{} "设置成功"
// @Router       /mag/organize/disable [post]
func (ctr *OrganizeManageController) DisableOrganize(ctx *gin.Context) {
	var req reqdata.SetStatusReq
	ctx.ShouldBind(&req)
	if req.Id == 0 {
		ctr.Response(ctx, respdata.CParamsInvalid)
		return
	}

	_, err := ctr.orgSrv.Enable(req.Id, req.Status)
	if err != nil {
		ctr.l.Error("set err %s", err)
		ctr.Response(ctx, respdata.CSysBusy)
		return
	}
	ctr.Response(ctx, respdata.CSuccess)
	return
}

// DeleteOrganize godoc
// @Summary      删除组织
// @Description  删除指定组织
// @Security     AdminKeyAuth
// @Tags         admin.organize
// @Accept       json
// @Produce      json
// @Param        param    body     reqdata.DetailReq  true  "指定管理员id"
// @success 200  {object} respdata.ResponseData{} "操作成功"
// @Router       /mag/organize/delete [post]
func (ctr *OrganizeManageController) DeleteOrganize(ctx *gin.Context) {
	var req reqdata.DetailReq
	ctx.ShouldBind(&req)
	if req.Id <= 0 {
		ctr.Response(ctx, respdata.CParamsInvalid)
		return
	}

	_, err := ctr.orgSrv.Delete(req.Id)
	if err != nil {
		ctr.l.Error("query err %s", err)
		ctr.Response(ctx, respdata.CSysBusy)
		return
	}

	ctr.Response(ctx, respdata.CSuccess)
}
